package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;
    /**
     * 实现关注/取关业务
     *
     * @param followUserId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {

        //获取用户id
        Long userId = UserHolder.getUser().getId();
        String key = "follows:"+userId;
        //判断是关注还是取关
        if (isFollow) {
            //关注
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            if (isSuccess) {
                //把关注的用户id放到set集合当中
                stringRedisTemplate.opsForSet().add(key, String.valueOf(followUserId));
            }
        } else {
            //取关，删除数据库中数据  new QueryWrapper<Follow>()，构造一个空的条件构造器，告诉要操作的表是Follow表,
            //条件是什么，就是.eq...
            boolean isSuccess = remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId)
                    .eq("follow_user_id", followUserId));
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
            }
        }
        return Result.ok();
    }

    /**
     * 判断是否关注博主
     *
     * @param followUserid
     * @return
     */
    @Override
    public Result isFollow(Long followUserid) {
        //获取用户id
        Long userId = UserHolder.getUser().getId();
        //根据用户id和被关注博主id来查询数据库是否关注
        Integer count = query().eq("user_Id", userId).eq("follow_user_id", followUserid).count();
        //如果数据库中有数据，说明已经关注了
        if (count > 0) {
            return Result.ok(true);
        } else {
            return Result.ok(false);
        }
    }

    /**
     * 查看共同关注
     * @param id //此id为点开所看用户的id
     * @return
     */
    @Override
    public Result followCommon(Long id) {
        //获取当前用户id
        Long userId = UserHolder.getUser().getId();
        String key1 = "follows:" + userId;
        String key2 = "follows:" + id;
        //判断是否有共同关注
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if(intersect == null || intersect.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        //解析id集合
        List<Long> ids = intersect.stream().map(s -> Long.valueOf(s)).collect(Collectors.toList());

        //根据解析出来的id进行查询用户
        List<Object> UserDTO = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        return Result.ok(UserDTO);
    }
}